#include "fonctions_player.h"
#include "structure_player.h"
#include "constantes_player.h"

void addActionList(listExecute *lac, player *p, player *o, actionMove movement)
{
	listExecuteAc **nll = (listExecuteAc **)malloc(sizeof(struct listExecuteAc *)*(lac->nbAction+1));
	listExecuteAc *nl = (listExecuteAc *)malloc(sizeof(struct listExecuteAc *));

	nl->movement=movement;
	nl->other=o;
	nl->player=p;
	nl->step=0;

	for(int i = 0; i < lac->nbAction; i++)
		nll[i]=lac->listAction[i];

	nll[lac->nbAction]=nl;
	lac->nbAction++;

	free((void *)lac->listAction);
	lac->listAction=nll;
}

void supActionList(listExecute *lac, int n)
{
	listExecuteAc **nll = (listExecuteAc **)malloc(sizeof(struct listExecuteAc *)*(lac->nbAction-1));

	for(int i = n+1; i < lac->nbAction; i++)
		nll[i-1]=lac->listAction[i];

	lac->nbAction--;

	free((void *)lac->listAction);
	lac->listAction=nll;
}

void executeActionList(listExecute *lac)
{
	for(int i=0; i <lac->nbAction;i++)
	{
		if (executeAction(lac->listAction[i]->player, *lac->listAction[i]->other,lac->listAction[i]->movement,lac->listAction[i]->step))
			supActionList(lac,i);
		else
			lac->listAction[i]->step++;
	}
}

void addAttackList(listExecute *lat, player *p, player *o, attack *att)
{
	listExecuteAt **nll = (listExecuteAt **)malloc(sizeof(struct listExecuteAt *)*(lat->nbAttack+1));
	listExecuteAt *nl = (listExecuteAt *)malloc(sizeof(struct listExecuteAt *));

	nl->attack=att;
	nl->other=o;
	nl->player=p;
	nl->step=0;

	for(int i = 0; i < lat->nbAttack; i++)
		nll[i]=lat->listAttack[i];

	nll[lat->nbAttack]=nl;
	lat->nbAttack++;

	free((void *)lat->listAttack);
	lat->listAttack=nll;
}
void supAttackList(listExecute *lat, int n)
{
	listExecuteAt **nll = (listExecuteAt **)malloc(sizeof(struct listExecuteAt *)*(lat->nbAttack-1));

	for(int i = n+1; i < lat->nbAttack; i++)
		nll[i-1]=lat->listAttack[i];

	lat->nbAttack--;

	free((void *)lat->listAttack);
	lat->listAttack=nll;
}

void executeAttackList(listExecute *lat)
{
	for(int i=0; i <lat->nbAttack;i++)
	{
		if (executeAttack(lat->listAttack[i]->player, *lat->listAttack[i]->other,lat->listAttack[i]->attack,lat->listAttack[i]->step))
			supAttackList(lat,i);
		else
			lat->listAttack[i]->step++;
	}
}